package com.franklin.ideaplugin.mybatisrunner.linemarker;

import com.franklin.ideaplugin.mybatisrunner.action.ExecuteSqlAction;
import com.franklin.ideaplugin.mybatisrunner.psi.MybatisPsiElement;
import com.franklin.ideaplugin.mybatisrunner.psi.MybatisXmlTag;
import com.intellij.codeInsight.daemon.LineMarkerInfo;
import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.editor.markup.GutterIconRenderer;
import com.intellij.pom.Navigatable;
import com.intellij.psi.PsiElement;
import com.intellij.psi.xml.XmlTag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Collection;

/**
 * @author Ye Junhui
 * @since 2024/2/19
 */
abstract class AbstractMapperLineMarkerInfo<T extends PsiElement> extends LineMarkerInfo<T> {

    protected T element;

    protected ExecuteSqlAction executeSqlAction;

    AbstractMapperLineMarkerInfo(
            @NotNull T element,
            @NotNull Icon icon,
            Navigatable navigatable,
            Collection<String> strings
    ) {
        super(
                element,
                element.getTextRange(),
                icon,
                psiAnnotation -> "Execute mybatis annotation sql",
                (mouseEvent, interfaceClassName) -> {

                },
                GutterIconRenderer.Alignment.CENTER,
                () -> {
                    return String.join(",", strings);
                }
        );
        this.element = element;
        this.executeSqlAction = new ExecuteSqlAction();
    }

    @Override
    public GutterIconRenderer createGutterRenderer() {
        return new LineMarkerGutterIconRenderer<>(this){

            @Override
            public AnAction getClickAction() {
                return executeSqlAction;
            }

            @Override
            public boolean isNavigateAction() {
                return true;
            }

            @Override
            public @Nullable ActionGroup getPopupMenuActions() {
                return null;
            }
        };
    }

    /**
     * 构建
     * @param t
     * @return
     */
    protected abstract MybatisPsiElement<T> buildMybatisPsiElement(T t);
}
